package com.wp.rcm.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class DemoFilter implements Filter{
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        //判断业务
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        if(httpRequest.getSession().getAttribute("loginUser")==null){
            PrintWriter out = httpResponse.getWriter();
            out.print("<script>");
            out.print("alert('请先登入');");
            out.print("location.href='"+httpRequest.getContextPath()+"/chance/toLogin'");
            out.print("</script>");
            out.flush();
            out.close();
            return ;
        }
        //通过==》进入请求
        chain.doFilter(httpRequest,httpResponse);
    }
}